<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单时间" prop="selDate">
        <el-date-picker
          v-model="queryParams.selDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="追缴状态" prop="recoveryStatus">
        <el-select v-model="queryParams.recoveryStatus" placeholder="请选择追缴状态" >
          <el-option
            label="未支付"
            value="1">
          </el-option>
          <el-option
            label="已完成"
            value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="artName" width="170"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" width="200" />
      <el-table-column label="追缴原因" align="center" prop="recoveryReason" width="300"/><el-table-column label="追缴状态"   align="center" prop="orderStatus" width="200">
        <template slot-scope="scope">
          {{ scope.row.recoveryStatus == '1' ? '未支付' : '已完成' }}
        </template>
      </el-table-column>
      <el-table-column label="订单日期" align="center" prop="createTime" width="200"/>
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {recoveryList, getOrder, delOrder, addOrder, updateOrder, send,refundZFBOrderOperate} from "@/api/system/order";
import dict from "@/utils/dict";
export default {
  dicts: ['courier_company',"order_status"],
  name: "Order",
  data() {
    return {
    // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      kdtitle:"",
      // 是否显示弹出层
      open: false,
      kdopen: false,
      // 查询参数
      queryParams: {
        selDate:"",
        orderStatus:"",
        orderType:"",
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "id",
        orderNo: null,
        orderName: null,
        orderRequestNo: null,
        orderDate: null,
        aliOrder: null,
        zmAuthOrderNo: null,
        aliTradeNo: null,
        wechatOrder: null,
        freezeAmount: null,
        unfreezeAmount: null,
        paymentAmount: null,
        refundAmount: null,
        orderAmount: null,
        parentOrderNo: null,
        artRentalId: null,
        artName: null,

        orderSource: null,
        orderSourceId: null,
        orderRemark: null,
        customerName: null,
        customerPhone: null,
        customerAddress: null,
        customerRemark: null,
        salesName: null,
        salesPhone: null,
        salesRemark: null,
        deliveryName: null,
        deliveryPhone: null,
        deliveryAddress: null,
        deliveryRemark: null,
        paymentMethod: null,
        paymentAccount: null,
        paymentRemark: null,
        logisticsName: null,
        logisticsNo: null,
        logisticsRemark: null,
        recoveryStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
      },
      kdrules: {
        kdCompany: [
          { required: true, message: "快递公司不能为空", trigger: "blur" }
        ],
        kdNumber: [
          { required: true, message: "快递单号不能为空", trigger: "blur" },
          { pattern: /^[a-zA-Z0-9]+$/, message: "快递单号格式不正确", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    kd100Change(e){

    let company =  this.dict.type.courier_company.find(res=>{
        return res.value == e
      })
      this.form.kdCompanyName = company.label
    },
    dict,
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      let send = JSON.parse(JSON.stringify(this.queryParams))
      this.queryParams.selDate
      if(this.queryParams.selDate && this.queryParams.selDate.length>1){
        send.startTime = this.queryParams.selDate[0]
        send.endTime = this.queryParams.selDate[1]
      }
      recoveryList(send).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    kdcancel() {
      this.kdopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        orderName: null,
        orderRequestNo: null,
        orderDate: null,
        aliOrder: null,
        zmAuthOrderNo: null,
        aliTradeNo: null,
        wechatOrder: null,
        freezeAmount: null,
        unfreezeAmount: null,
        paymentAmount: null,
        refundAmount: null,
        orderAmount: null,
        parentOrderNo: null,
        artRentalId: null,
        artName: null,
        orderType: null,
        orderStatus: null,
        orderSource: null,
        orderSourceId: null,
        orderRemark: null,
        customerName: null,
        customerPhone: null,
        customerAddress: null,
        customerRemark: null,
        salesName: null,
        salesPhone: null,
        salesRemark: null,
        deliveryName: null,
        deliveryPhone: null,
        deliveryAddress: null,
        deliveryRemark: null,
        paymentMethod: null,
        paymentAccount: null,
        paymentRemark: null,
        logisticsName: null,
        logisticsNo: null,
        logisticsRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    kd100(row){
      this.kdcancel();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.kdopen = true;
        this.kdtitle = "发货设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    sendInfo(){
      this.$refs["formkd100"].validate(valid => {
        if (valid) {
          send(this.form).then(response => {
            this.$modal.msgSuccess("设置成功");
            this.kdopen = false;
            this.getList();
          });
        }else{
          this.$modal.msgError("验证失败");
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
